<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html lang="zh-CN">
<%@include file="include-head.jsp" %>
<link rel="stylesheet" href="css/pagination.css">
<script type="text/javascript" src="jquery/jquery.pagination.js"></script>
<link rel="stylesheet" href="ztree/zTreeStyle.css">
<script type="text/javascript" src="ztree/jquery.ztree.all-3.5.min.js"></script>
<body>
<%@include file="include-nav.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%@include file="include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input type="text" style="display:none" />
                                <input id="keyWordInput" class="form-control has-success" type="text" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button type="button" id="searchBtn" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询</button>
                    </form>
                    <button type="button" id="removeModalBtn" class="btn btn-danger" style="float:right;margin-left:10px;"><i class=" glyphicon glyphicon-remove"></i> 删除</button>
                    <button type="button" id="showAddModalBtn" class="btn btn-primary" style="float:right;" ><i class="glyphicon glyphicon-plus"></i> 新增</button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input type="checkbox" name="selectall"></th>
                                <th>名称</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="rolePageBody">


                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                </td>
                            </tr>

                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%@include file="modal-role-add.jsp"%>
<%@include file="modal-role-edit.jsp"%>
<%@include file="modal-role-confirm.jsp"%>
<%@include file="modal-role-assign-auth.jsp"%>
</body>
<script type="text/javascript" src="crowd/my-role.js"></script>
<script>
    $(function () {
        window.pageNum = 1;
        window.pageLimit = 5;
        window.keyWord = "";

        // 调用执行分页的函数，显示分页效果
        generatePage();

        //给查询按钮绑定函数
        $("#searchBtn").click(function () {
            // 获取关键词数据
            window.keyWord = $("#keyWordInput").val();

            // 查询之后回到第一页
            window.pageNum = 1;
            // 调用分页函数刷新页面
            generatePage();
        });

        // 点击新增按钮打开模态框
        $("#showAddModalBtn").click(function () {
            $("#addModal").modal("show")
        })

        // 新增功能保存函数
        $("#saveRoleBtn").click(function () {
            // 获取文本框的角色名称
            var roleName = $("#addModal [name=roleName]").val();

            // 发送请求
            $.ajax({
                url:'role/save',
                type:'post',
                data:{
                    name:roleName
                },
                dataType:'json',
                success:function (res) {


                    if(res.result == "SUCCESS"){
                        layer.msg("操作成功");

                        //重新加载分页
                        window.pageNum = 999999;
                        generatePage();
                    }
                    if(res.result == "ERROR"){
                        layer.msg("操作失败:"+res.message);
                    }
                },
                error:function (res) {
                    layer.msg(res.statusText+"状态码:"+res.status)
                }
            })
            // 关闭模态框
            $("#addModal").modal("hide");
            // 清理模态框
            $("#addModal [name=roleName]").val("");


        })
        // 修改按钮
        $("#rolePageBody").on("click",".pencilBtn",function () {
            window.roleId = $(this).attr('value');

            $("#editModal").modal("show");

            var roleName = $(this).parent().prev().text();
            $("#editModal [name=roleName]").val(roleName);
        })

        // 更新按钮绑定更新函数
        $("#updateRoleBtn").click(function () {
            var roleName = $("#editModal [name=roleName]").val();
            $.ajax({
                url:'role/update',
                type:'post',
                data:{
                    name:roleName,
                    id:window.roleId
                },
                dataType:'json',
                success:function (res) {
                    if(res.result == "SUCCESS"){
                        layer.msg("操作成功");

                        //重新加载分页
                        generatePage();
                    }
                    if(res.result == "ERROR"){
                        layer.msg("操作失败:"+res.message);
                    }
                },
                error:function (res) {
                    layer.msg(res.statusText+"状态码:"+res.status)
                }

            })
            // 关闭模态框
            $("#editModal").modal("hide");
        })


        // 点击确认模态框中的确认删除
        $("#removeRoleBtn").click(function () {
            var roleIds = window.roleIds;
            var requestBody = JSON.stringify(roleIds);
            $.ajax({
                url:'role/delete/by/role/id/array',
                type:'post',
                data:requestBody,
                contentType:'application/json;charset=UTF-8',
                dataType:'json',
                success:function (res) {
                    if(res.result == "SUCCESS"){
                        layer.msg("操作成功");

                        //重新加载分页
                        generatePage();
                    }
                    if(res.result == "ERROR"){
                        layer.msg("操作失败:"+res.message);
                    }
                },
                error:function (res) {
                    layer.msg(res.statusText+"状态码:"+res.status)
                }
            })
            // 关闭模态框
            $("#confirmModal").modal("hide");
        })

        // 给单条按钮绑定响应函数
        $("#rolePageBody").on("click",'.removeBtn',function () {
            var roleId = $(this).attr('value');


            var roleName = $(this).parent().prev().text();
            // 创建一个role对象
            var roleArray = [{
                roleId:roleId,
                roleName:roleName
            }];

            showConfirmModal(roleArray)
        })
        /*批量选中的效果*/
        $('input:checkbox[name="selectall"]').click(function(){
            if($(this).is(':checked')){
                $('input:checkbox').each(function(){
                    $(this).prop("checked",true);
                });
            }else{
                $('input:checkbox').each(function(){
                    $(this).prop("checked",false);
                });
            }
        });
        // 批量删除按钮绑定函数
        $("#removeModalBtn").click(function () {
            var roleArray = [];
          $("input[name='checkId']:checked").each(function () {
              var roleId = $(this).attr('value');
              var roleName = $(this).parent().next().text();

              roleArray.push({
                  roleId:roleId,
                  roleName:roleName
              });
          })
          if(roleArray.length == 0){
              layer.msg("请至少选择一个！执行删除");
              return;
          }
          showConfirmModal(roleArray);
        })

        // 给分配权限按钮增加单击函数
        $("#rolePageBody").on("click",".checkBtn",function () {
            window.roleId = $(this).attr("value");

            $("#assignModal").modal("show");

            // 在模态框中装载树形结构
            fillAuthTree();
        })
        $("#assignBtn").click(function () {

            var authIdArray = [];

            // 获取zTreeObject对象
            var zTreeObj = $.fn.zTree.getZTreeObj("authTreeDemo");

            // 获取被勾选的节点
            var checkedNodes = zTreeObj.getCheckedNodes();

            // 遍历这个checkedNodes
            for (var i = 0; i < checkedNodes.length; i++){
                var checkNode = checkedNodes[i];
                var authId = checkNode.id;

                authIdArray.push(authId);
            }
            // 发送请求
            var requestBody = {
                authIdArray: authIdArray,
                roleId: [window.roleId]
            };
            requestBody = JSON.stringify(requestBody);
            $.ajax({
                url: "assign/do/role/assign/auth",
                type: "post",
                data: requestBody,
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success:function (res) {
                    if(res.result == "SUCCESS"){
                        layer.msg("操作成功")
                    }
                    if(res.result == "ERROR"){
                        layer.msg(res.message)
                    }
                },
                error:function (res) {
                    layer.msg(res.statusText+"状态码:"+res.status)
                }
            });
            $("#assignModal").modal("hide");
        })
    })
</script>
</html>
